package com.endhiroid.livewallpaper;

import java.io.File;
import java.io.FileFilter;

import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.PreferenceActivity;
import android.util.AttributeSet;

public class EndhiroidLiveWallpaperSettings extends PreferenceActivity implements
    SharedPreferences.OnSharedPreferenceChangeListener {
  public static final String ROTATE_KEY             = "rotate";
  public static final String FOLDER_KEY             = "folder";
  public static final String TIMER_KEY              = "timer";
  public static final String SCROLLING_KEY          = "scrolling";
  public static final String IS_MOUNTED             = "isMounted";
  public static final String STRETCHING_KEY         = "stretching";
  public static final String TRIM_KEY               = "trim";
  public static final String FOLDER_OI_KEY          = "folder_oi";
  public static final String CLICK_TO_CHANGE_KEY    = "clickToChange";
  private static final int   FOLDER_OI_REQUEST_CODE = 1;
  public static final int    GALLERY_REQUEST_CODE   = 2;
  public static final String TRANSITION_KEY         = "transition";
  public static final int    FADE_TRANSITION        = 1;
  public static final int    NO_TRANSITION          = 0;


  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // this is the best way I've found to disable the stupid transparent
    // background
    getListView().setBackgroundColor(Color.WHITE);
    getListView().setCacheColorHint(Color.WHITE);


    getPreferenceManager().setSharedPreferencesName(
        EndhiroidLiveWallpaper.GalleryEngine.SHARED_PREFS_NAME);
    addPreferencesFromResource(R.xml.wallpaper_settings);

   
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));

   
    DialogPreference dialogPreference = new BasicDialogPreference(this, null);
    dialogPreference.setTitle(R.string.help);
    dialogPreference.setSummary(R.string.help_summary);
    dialogPreference.setDialogMessage(getHelpMessage());
    getPreferenceScreen().addPreference(dialogPreference);

    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }

  private CharSequence getHelpMessage() {
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    return new StringBuilder()
        .append("Virtual Screen Width : ")
        .append(wallpaperManager.getDesiredMinimumWidth())
        .append('\n')
        .append("Virtual Screen Height: ")
        .append(wallpaperManager.getDesiredMinimumHeight())
        .append('\n')
        .append("Visit:\n")
        .append("http://endhiroid.blogspot.com")
        .toString();
  }

  /**
   * Only want directories
   */
  private static final FileFilter DIRECTORY_FILTER = new FileFilter() {
                                                     @Override
                                                     public boolean accept(File pathname) {
                                                       return pathname.isDirectory();
                                                     }

                                                   };


  @Override
  protected void onResume() {
    super.onResume();
  }

  @Override
  protected void onDestroy() {
    getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    super.onDestroy();
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  }


  /**
   * Handle the OI Folder result
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FOLDER_OI_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
      String filename = data.getDataString();
      if (filename != null) {
        // Get rid of URI prefix:
        if (filename.startsWith("file://")) {
          filename = filename.substring(7);
        }
        // Log.v("wallpaper", "Saving folder preference using OI File Manager");
        getPreferenceManager().getSharedPreferences().edit().putString(FOLDER_KEY, filename)
            .commit();
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private class BasicDialogPreference extends DialogPreference {
    public BasicDialogPreference(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
    }

    public BasicDialogPreference(Context context, AttributeSet attrs) {
      super(context, attrs);
    }
  }
}